<?php

namespace app\admin\controller\mk;
use app\admin\model\ShopSetting as ss;
use app\common\controller\Backend;

/**
 * 首单优惠
 */
class Firstorder extends Backend
{
    /**
     * Sign模型对象
     * @var object
     * @phpstan-var \app\admin\model\mk\Sign
     */
    protected object $model;
 
     public function setting(){
           $data = input("post.data");
           $act = input("post.act");
           switch ($act) {
               case 'post':
                   $data = input("post.data");
                   $status = $data['status'] ?? 0;
                   $type = input("post.type");
                   if(isset($data['status']) && $data['status'] == 1){
                   		if(!isset($data['type']) || $data['type'] == ''){
                   			   $this->success("ok",['code'=>0,'msg'=>'请选择优惠类型']);
                   		}
                   		if(isset($data['type']) && $data['type'] == 1){
                   			   if(!isset($data['money'])){
                   			   	  $this->success("ok",['code'=>0,'msg'=>'请输入立减金额']);
                   			   }
                   			   if($data['money'] == '' || $data['money'] == 0){
                   			   		$this->success("ok",['code'=>0,'msg'=>'请输入立减金额']);
                   			   }
                   		}
                   		if(isset($data['type']) && $data['type'] == 2){
 							   if(!isset($data['discount'])){
                   			   	  $this->success("ok",['code'=>0,'msg'=>'请输入折扣']);
                   			   }
                   			   if($data['discount'] == '' || $data['discount'] == 0){
                   			   		$this->success("ok",['code'=>0,'msg'=>'请输入折扣']);
                   			   }

                   		}
                   }
                   
                   $find = ss::where("type",'firstorder')->find();
 					
                   if($find){
                   	     unset($data['status']);
                         ss::where("type",'firstorder')->update(['settings'=>json_encode($data),'status'=>$status]);
                   }else{
                         ss::insert([
                                'status'=>$status,
                                'type'=>'firstorder',
                                'settings'=>json_encode($data)
                         ]);
                   }
                   $this->success("ok",['code'=>1,'msg'=>'成功']);
                   break;
               
               default:
                   $find = ss::where("type",'firstorder')->find();
                   $arr = [];
                   if($find){
                   	 $arr = json_decode($find['settings'],true);
                   }
                   $arr['status'] = ($find['status'] ?? 0) == 1?true:false;
                   $arr['type'] = intval($arr['type']??0);
                   $arr['first_pro'] = intval($arr['first_pro'] ?? 0) == 1?true:false;
                   $this->success("ok",['code'=>1,'data'=>$arr,'status'=>$find['status']??'']);
                   break;
           }
           
            
    }


}